package com.ruoyi.web.controller.business;

import cn.hutool.core.text.CharSequenceUtil;
import com.ruoyi.business.bo.HcxMerchantTypeAddBo;
import com.ruoyi.business.bo.HcxMerchantTypeEditBo;
import com.ruoyi.business.bo.HcxMerchantTypeQueryBo;
import com.ruoyi.business.service.IHcxMerchantTypeService;
import com.ruoyi.business.vo.HcxMerchantTypeVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.HcxMerchantType;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商家分类Controller
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Api(value = "商家分类控制器", tags = {"商家分类管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/MerchantType")
public class HcxMerchantTypeController extends BaseController {

    private final IHcxMerchantTypeService iHcxMerchantTypeService;

    /**
     * 查询商家分类列表
     */
    @ApiOperation("查询商家分类列表")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:list')")
    @GetMapping("/list")
    public AjaxResult<List<HcxMerchantTypeVo>> list(@Validated HcxMerchantTypeQueryBo bo) {
        List<HcxMerchantTypeVo> list = iHcxMerchantTypeService.queryList(bo);
        return AjaxResult.success(list);
    }

    /**
     * 查询商家分类列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('business:MerchantType:list')")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult<List<HcxMerchantType>> excludeChild(@PathVariable(value = "id", required = false) Long id) {
        List<HcxMerchantType> depts = iHcxMerchantTypeService.selectMerchantTypetList(new HcxMerchantType());
        depts.removeIf(d -> d.getId().intValue() == id
                || ArrayUtils.contains(CharSequenceUtil.splitToArray(d.getAncestors(), ","), id + ""));
        return AjaxResult.success(depts);
    }


    /**
     * 导出商家分类列表
     */
    @ApiOperation("导出商家分类列表")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:export')")
    @Log(title = "商家分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxMerchantTypeQueryBo bo) {
        List<HcxMerchantTypeVo> list = iHcxMerchantTypeService.queryList(bo);
        ExcelUtil<HcxMerchantTypeVo> util = new ExcelUtil<>(HcxMerchantTypeVo.class);
        return util.exportExcel(list, "商家分类");
    }

    /**
     * 获取商家分类详细信息
     */
    @ApiOperation("获取商家分类详细信息")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxMerchantTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxMerchantTypeService.queryById(id));
    }

    /**
     * 新增商家分类
     */
    @ApiOperation("新增商家分类")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:add')")
    @Log(title = "商家分类", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxMerchantTypeAddBo bo) {
        return toAjax(iHcxMerchantTypeService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商家分类
     */
    @ApiOperation("修改商家分类")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:edit')")
    @Log(title = "商家分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxMerchantTypeEditBo bo) {
        return toAjax(iHcxMerchantTypeService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商家分类
     */
    @ApiOperation("删除商家分类")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:remove')")
    @Log(title = "商家分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ids) {
        return toAjax(iHcxMerchantTypeService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入商家分类数据
     */
    @ApiOperation("导入商家分类数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:MerchantType:import')")
    @Log(title = "商家分类", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxMerchantTypeVo> util = new ExcelUtil<>(HcxMerchantTypeVo.class);
        List<HcxMerchantTypeVo> hcxMerchantTypeList;
        try {
            hcxMerchantTypeList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxMerchantTypeService.importHcxMerchantType(hcxMerchantTypeList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxMerchantTypeVo> util = new ExcelUtil<>(HcxMerchantTypeVo.class);
        return util.importTemplateExcel("商家分类数据");
    }


    @GetMapping("/treeselect")
    public AjaxResult<List<TreeSelect>> treeselect(HcxMerchantType hcxMerchantType) {
        List<HcxMerchantType> list = iHcxMerchantTypeService.selectMerchantTypetList(hcxMerchantType);
        return AjaxResult.success(iHcxMerchantTypeService.buildMerchantTypeTreeSelect(list));
    }

}
